﻿using System;
using AcceptanceTests.Helpers;
using NUnit.Framework;
using QuantoSharp.Core;
using TechTalk.SpecFlow;

namespace AcceptanceTests
{
    [Binding]
    public class SharedSteps
    {

        private Array _cashFlow;
        private Array _cashFlowTime;

        [Given(@"I have the following input:")]
        public void GivenIHaveTheFollowingInput(Table table)
        {
            _cashFlow = Array.CreateInstance(typeof(double), table.RowCount);
            _cashFlowTime = Array.CreateInstance(typeof(double), table.RowCount);

            for (int i = 0; i < table.RowCount; i++)
            {
                _cashFlow.SetValue(Convert.ToDouble(table.Rows[i]["CashFlow"]), i);
                _cashFlowTime.SetValue(Convert.ToDouble(table.Rows[i]["Time"]), i);
            }
        }

        [Given(@"the annual interest rate is (.*)")]
        public void GivenTheAnnualInterestRateIs(double rate)
        {
            ScenarioContext.Current.Add("ROR", rate);
        }

        [When(@"I invoke (.*)")]
        public void WhenIInvoke(string methodName)
        {
            var annualRateOfReturn = ScenarioContext.Current["ROR"];

            var result = AssertMethodInvoker.Invoke<PriceCalculator>(methodName, _cashFlowTime, _cashFlow,
                                                                     annualRateOfReturn);
            ScenarioContext.Current.Add("Result", result);
        }

        [Then(@"I should expect (.*)")]
        public void ThenIShouldExpectResult(string expectation)
        {
            Assert.That(ScenarioContext.Current.ContainsKey("Result"));

            var actualResult = ScenarioContext.Current["Result"];

            Assert.AreEqual(expectation, actualResult.ToString());
        }

    }
}
